﻿using System.Configuration.Install;
using System.ServiceProcess;

namespace ThinkAway.Plus.Office
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string[] args)
        {
            //ProjectService projectService = new ProjectService();
            //projectService.Start();

            if (args.Length > 0)
            {
                string command = args[0];
                switch (command.ToLower())
                {
                    case "-i":
                    case "/i":
                        RegistryServer();
                        break;
                    case "-u":
                    case "/u":
                        UninstallServer();
                        break;
                    default:
                        System.Console.WriteLine("Unknow command line.");
                        break;
                }
            }
            else
            {
                ServiceBase[] ServicesToRun = new ServiceBase[] 
                                              { 
                                                  new ProjectService() 
                                              };
                ServiceBase.Run(ServicesToRun);
            }

        }

        static void RegistryServer()
        {
            string[] cmdline = { };
            string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;

            TransactedInstaller transactedInstaller = new TransactedInstaller();
            AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
            transactedInstaller.Installers.Add(assemblyInstaller);
            transactedInstaller.Install(new System.Collections.Hashtable());
        }

        static void UninstallServer()
        {
            string[] cmdline = { };
            string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;

            TransactedInstaller transactedInstaller = new TransactedInstaller();
            AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
            transactedInstaller.Installers.Add(assemblyInstaller);
            transactedInstaller.Uninstall(null);
        }
    }
}
